<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <div class="tab active">账号密码登录</div>
        <div class="tab">手机号登录</div>
      </div>
      <div class="login-form">
        <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules">
          <div class="input-group">
            <i class="iconfont icon-zhanghao"></i>
            <el-form-item prop="username">
              <input type="text" v-model="ruleForm.username" placeholder="账号：limengyue">
            </el-form-item>
          </div>
          <div class="input-group ">
            <i class="iconfont icon-mima"></i>
            <el-form-item prop="password">
              <input type="password" v-model="ruleForm.password" placeholder="密码：0r1u.v>J">
            </el-form-item>
          </div>
          <div class="input-group1 captcha-group">
            <el-form-item prop="captcha">
              <input type="text" v-model="ruleForm.captcha" placeholder="验证码">
            </el-form-item>
            <img :src="captchaImg.src" @click="refreshCaptcha" class="captcha-img" >
          </div>
        </el-form>
        <div class="options-row">
          <div class="remember">
            <input type="checkbox" v-model="autoLogin">
            <label for="auto-login">自动登录</label>
          </div>
          <a href="#" class="forgot-password">忘记密码</a>
        </div>
        <button class="login-btn" @click="submitForm(ruleFormRef)">登录</button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref,onMounted} from 'vue'
import { useRouter } from 'vue-router'
import { loginApi,getCaptcha } from '@/api/login';

const router = useRouter()
const ruleForm = reactive({
  captcha_id:'',
  captcha_code:'',
  captcha:'',
  username:'',
  password:'',
})

const ruleFormRef = ref()
const autoLogin = ref(false)
const captchaImg = reactive({src: '', id: ''})

const checkCaptcha = (rule,value,callback)=>{
  if(value){
    callback()
  }else{
    callback(new Error('请输入验证码'))
  }
}

const checkPass = (rule,value,callback)=>{
  if(value){
    callback()
  }else{
    callback(new Error('请输入密码'))
  }
}

const checkUserName = (rule,value,callback)=>{
  const regexp = /[a-zA-X0-9]+$/
  if(regexp.test(value)){
    callback()
  }else{
    callback(new Error('请输入11位手机号码'))
  }
}

const rules = reactive({
  captcha:[{validator:checkCaptcha,trigger:'blur'}],
  username:[{validator:checkUserName,trigger:'blur'}],
  password:[{validator:checkPass,trigger:'blur'}]
})

const logindata=ref()

const submitForm = (formEl) => {
  if(!formEl) return
  formEl.validate((valid)=>{
    if(valid){
      loginApi({
        username:ruleForm.username,
        password:ruleForm.password,
        captcha_code:ruleForm.captcha,
        captcha_id:ruleForm.captcha_id
      }).then(res=>{
        logindata.value = res.data.info
        localStorage.setItem('login',JSON.stringify(res.data))
        router.replace('/newspool')
        console.log(logindata);
      }).catch(error => {
        console.error("登录失败:", error);
        // 登录失败时刷新验证码
        refreshCaptcha();
        // 清空验证码输入框
        ruleForm.captcha = '';
      })
    }else{
      console.log('error submit');
      // 表单验证失败时也刷新验证码
      refreshCaptcha();
      // 清空验证码输入框
      ruleForm.captcha = '';   
    }
  })
}

const refreshCaptcha = () => {
  getCaptcha().then((res)=>{
    console.log("验证码API返回数据:", res);
    if(res && res.data) {
      captchaImg.src = res.data.image;
      captchaImg.id = res.data.id;
      ruleForm.captcha_id = res.data.id;
    } else {
      console.error("获取验证码失败，返回数据格式不正确");
    }
  }).catch(error => {
    console.error("获取验证码失败:", error);
  });
}

onMounted(()=>{
  refreshCaptcha()
})

</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 420px;
  margin-top: 50vh;
  translate: 0 -50%;
  background: url("../../assets//backgroud.svg") no-repeat;
  background-size: cover;
}

.login-box {
  width: 420px;
  border-radius: 12px;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
  padding: 40px 30px;
  backdrop-filter: blur(10px);
}

.login-header {
  display: flex;
  justify-content: center;
  margin-bottom: 30px;
}

.tab {
  width: 140px;
  text-align: center;
  padding: 15px 0;
  font-size: 16px;
  color: #666;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
}

.tab.active {
  color: #409EFF;
}

.tab.active::after {
  content: "";
  position: absolute;
  bottom: -1px;
  left: 0;
  width: 100%;
  height: 3px;
  background-color: #409EFF;
  border-radius: 3px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.input-group {
  position: relative;
  display: flex;
  align-items: center;
}

.icon-zhanghao, .icon-mima {
  position: absolute;
  top: 9px;
  left: 15px;
  color: #999;
  font-size: 18px;
  z-index: 1;
}

.input-group input {
  width: 360px;
  height: 48px;
  padding: 0 15px 0 45px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 15px;
  transition: all 0.3s;
  background-color: #f9f9f9;
}

.input-group1 input {
  height: 48px;
  padding: 0 15px 0 45px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 15px;
  transition: all 0.3s;
  background-color: #f9f9f9;
}

input:focus {
  outline: none;
  border-color: #409EFF;
  background-color: #fff;
  box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
}

input::placeholder {
  color: #bbb;
}

.options-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.remember {
  display: flex;
  align-items: center;
  font-size: 14px;
  color: #666;
}

.remember input[type="checkbox"] {
  width: 16px;
  height: 16px;
  margin-right: 6px;
  cursor: pointer;
}

.forgot-password {
  color: #409EFF;
  text-decoration: none;
  font-size: 14px;
}

.captcha-group {
  display: flex;
  gap: 10px;
}

.captcha-img {
  height: 48px;
  width: 120px;
  border: 1px solid #ddd;
  border-radius: 8px;
  cursor: pointer;
}

.login-btn {
  width: 100%;
  height: 48px;
  background: #409EFF;
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  cursor: pointer;
  margin-top: 10px;
}
</style>
